from django.db import models
from django.conf import settings

class DeliveryArea(models.Model):
    name = models.CharField(max_length=100, verbose_name='区域名称')
    description = models.TextField(blank=True, null=True, verbose_name='区域描述')
    status = models.CharField(max_length=10, choices=[
        ('active', '激活'),
        ('inactive', '禁用')
    ], default='active', verbose_name='状态')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '配送区域'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return self.name

class DeliverymanArea(models.Model):
    deliveryman = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='delivery_areas', verbose_name='配送员')
    area = models.ForeignKey(DeliveryArea, on_delete=models.CASCADE, related_name='deliverymen', verbose_name='配送区域')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '配送员区域关联'
        verbose_name_plural = verbose_name
        unique_together = ('deliveryman', 'area')
        
    def __str__(self):
        return f'{self.deliveryman.username} - {self.area.name}'

class DeliveryTask(models.Model):
    STATUS_CHOICES = (
        ('pending', '待处理'),
        ('assigned', '已分配'),
        ('rejected', '已拒绝'),  # 新增状态
        ('awaiting_manual_assignment', '等待手动分配'), # 新增状态
        ('delivering', '配送中'),
        ('completed', '已完成'),
        ('cancelled', '已取消'),
    )
    
    PRIORITY_CHOICES = (
        ('high', '高'),
        ('medium', '中'),
        ('low', '低'),
    )
    
    delivery_number = models.CharField(max_length=50, unique=True, verbose_name='配送编号')
    sales_order = models.OneToOneField('orders.Order', on_delete=models.CASCADE, related_name='delivery_task', verbose_name='销售订单')
    deliveryman = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=True, related_name='delivery_tasks', verbose_name='配送员')
    status = models.CharField(max_length=30, choices=STATUS_CHOICES, default='pending', verbose_name='状态') # 调整 max_length
    priority = models.CharField(max_length=10, choices=PRIORITY_CHOICES, default='medium', verbose_name='优先级')
    rejection_count = models.PositiveIntegerField(default=0, verbose_name='拒绝次数') # 新增字段
    estimated_delivery_time = models.DateTimeField(null=True, blank=True, verbose_name='预计配送时间')
    actual_delivery_time = models.DateTimeField(null=True, blank=True, verbose_name='实际配送时间')
    note = models.TextField(blank=True, null=True, verbose_name='备注')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_at = models.DateTimeField(auto_now=True, verbose_name='更新时间')
    
    class Meta:
        verbose_name = '配送任务'
        verbose_name_plural = verbose_name
        indexes = [
            models.Index(fields=['status'], name='idx_delivery_tasks_status'),
            models.Index(fields=['deliveryman'], name='idx_delivery_tasks_deliveryman'),
        ]
        
    def __str__(self):
        return f'配送任务 #{self.delivery_number}'

class DeliveryStatusRecord(models.Model):
    delivery_task = models.ForeignKey(DeliveryTask, on_delete=models.CASCADE, related_name='status_records', verbose_name='配送任务')
    status = models.CharField(max_length=30, choices=DeliveryTask.STATUS_CHOICES, verbose_name='状态') # 调整 max_length
    operator = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='delivery_status_records', verbose_name='操作人')
    location = models.CharField(max_length=255, blank=True, null=True, verbose_name='位置')
    note = models.TextField(blank=True, null=True, verbose_name='备注')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    
    class Meta:
        verbose_name = '配送状态记录'
        verbose_name_plural = verbose_name
        
    def __str__(self):
        return f'{self.delivery_task.delivery_number} - {self.status}'
